<?php

namespace App\Models\DylistModels;

use App\Models\ParentModel;
use ClassesWithParents\D;
use DB;
use App\Components\Tools;
use Log;

/**
 * 抖音实时热点词聚合视频
 * @author puppet
 */
class DyHotsearchWord extends ParentModel
{
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'pt_dy_hotsearch_word';

    /**
     * DB::直接操作的数据库表名
     * @var string
     */
    protected $db_table = 'pt_dy_hotsearch_word';

    /**
     * 主键 默认id。
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 指定是否模型应该被戳记时间。
     *
     * @var bool
     */
    public $timestamps = false;

//获取当天的所有不重复的热点词
    function  getword(){
        $sql=" select id,sentence from $this->table where to_days(create_time) = to_days(now())";
        $sql.=" group by sentence order by hot_level desc limit 50 ";
        // print_r($sql);
        $wordsinfo = DB::select($sql);

        return $wordsinfo;
    }
    //获取当天rank1的热度指数值
    function getMaxValue()
    {
        $sql = " select max(hot_level) from $this->table  where to_days(create_time) = to_days(now())";
        $maxValue = DB::select($sql);
        //   print_r($maxValue);exit();
        if (empty($maxValue)) {//如果当日数据为空 就显示昨天的数据
            $sqll = " select max(hot_level) from $this->table where  TO_DAYS( NOW( ) ) - TO_DAYS( create_time) = 1";

            $maxValue = DB::select($sqll);
        }
        return $maxValue[0]['max(hot_level)'];
    }
    //插入热点词榜数据
    function upHotsearchWord($date)
    {
        set_time_limit(0);
        foreach ($date as $k => $v) {
            $data = array(
                'hot_level' => $v['hot_level'],
                'sentence' => $v['sentence'],
                'create_time' => date('Y-m-d H:i:s', time()),
                'update_time' => date('Y-m-d H:i:s', time()),
            );
            DB::table($this->table)->insertGetId($data);

        }
    }

}